'use strict'

const { TaiSpider } = require('tai-spider');
const { formatDate } = require('../utils/str-helper');

const catids = [{
    id: 3486,
    title: 'launch',
}, {
    id: 3551,
    title: 'ground systems',
}, {
    id: 1367,
    title: 'government military'
}];

class SatnewsFullSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'satnews-full';
        this.debug = true;
        this.start_urls = [
            'https://news.satnews.com'
        ];
    }

    *parse(response) {
        for (let ele of response.css('.widget_archive li')) {
            yield response.follow(ele.css('a'), this.parseArchive);
        }
    }

    *parseArchive(response) {
        let firstPara = response.css('.archive-pagination').get(0);
        yield* response.follow_all(firstPara.css('a'), this.parseArchive)

        for (let ele of response.css('article')) {
            yield response.follow(ele.css('.entry-title a'), this.parseArticle);
        }
    }

    *parseArticle(response, spider) {
        let data = response.extract();
        let date = formatDate(data.date)
        data.timestamp = date.valueOf();
        data.date = date.format();

        yield Object.assign({
            index_name: 'tai-article',
        }, data);
    }
}

module.exports = SatnewsFullSpider;